#include "BBuilder.h"

BBuilder::BBuilder(glm::vec3 _Pos, float _r, std::string _Name,
		std::string _modelFName, ModelPool *_mPool, PathFinder *_pathfinder) :
		Building(_Pos, _r, _Name, _modelFName, _mPool, _pathfinder)
{
	type = OBJ_BBUILDER;
	parentType = OBJ_BUILDING;
}

BBuilder::~BBuilder(void)
{
}

void BBuilder::control(float t)
{
	if ((underConstruction) && timer.GetInterval(10))
		updateAge();

	if ((slaves.size() > 0) && chckAreaTimer.GetInterval(5000))
	{
		for (unsigned int i = 0; i < slaves.size(); i++)
		{
			if (slaves[i]->getType() == OBJ_BUILDER)
			{
				Builder *builder = static_cast<Builder*>(slaves[i]);
				if ((builder->getStatus() == STATUS_READY)
						|| (builder->getStatus() == STATUS_GOHOME))
				{

					// look for building with status placed
					void *ent = pathfinder->getNearestEntityOfType(gPoint, activityRadius,
							OBJ_BUILDING, STATUS_PLACED, true);
					if (ent != NULL)
					{
						// set job to builder inside this building
						Building *building = static_cast<Building*>(ent);
						builder->setWork(building);

						gCon::con->print("%s: Found entity for construction on %f, %f",
								Name.c_str(), building->get2dPosition().x,
								building->get2dPosition().y);
					}
				}
			}
		}
	}

}